//
//  BanBu_UpgradeDataBase.m
//  BanBu
//
//  Created by Wang Xj on 13-12-5.
//
//

#import "BanBu_UpgradeDataBase.h"
#import "AppDataManager.h"
#import "FMDatabase.h"

//升级数据库
@implementation BanBu_UpgradeDataBase

//数据库迁移
+(BOOL)backupDB
{
    BOOL bl;
    //备份数据库
    bl = [BanBu_UpgradeDataBase backupDataBase];
    
    //2删除原数据库
    bl = [BanBu_UpgradeDataBase deleteOriginalDataBase];
    
    //3创建新的数据库
    bl = [BanBu_UpgradeDataBase initNewDataBase];
    
    //4将备份数据库中的数据拷贝到新数据库中
    bl = [BanBu_UpgradeDataBase copyDataToNewDB];
    
    //5删除备份数据库
    bl = [BanBu_UpgradeDataBase deleteBackupDataBase];
    
    return bl;
}

+(NSString *)getBackupTableName:(NSString *)tableName
{
    NSString * backupTableName = [NSString stringWithFormat:@"%@_bak",tableName];
    
    return backupTableName;
}

//备份数据库
+(BOOL)backupDataBase
{
    if (![MyAppDataManager.dataBase goodConnection])
    {
        [MyAppDataManager.dataBase open];
    }
    
    //对话表
    NSString * talkPeoplesTable = [self getBackupTableName:TALKPEOPLES];
    NSString * query = [NSString stringWithFormat:@"CREATE table %@ AS SELECT * FROM %@",talkPeoplesTable,TALKPEOPLES];
    
    BOOL bl = [MyAppDataManager.dataBase executeUpdate:query];
    if (!bl)
    {
        NSLog(@"backupDataBase :%@",[[MyAppDataManager.dataBase lastError]description]);
    }
//    file:///Users/banbu3/Desktop/%E9%85%B7%E8%81%8A%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC/BanBu/Buddy/verify_card.png
    //聊天记录表
    NSString * chatTable;
    for (int i = 0 ; i < 10; i++)
    {
        NSString * oldChatTable = [MyAppDataManager getTableNameForUid:[NSString stringWithFormat:@"%d",i]];
        chatTable = [self getBackupTableName:oldChatTable];
        query = [NSString stringWithFormat:@"CREATE table %@ AS SELECT * FROM %@",chatTable,oldChatTable];
        bl = [MyAppDataManager.dataBase executeUpdate:query];
        if (!bl)
        {
            NSLog(@"backupDataBase :%@",[[MyAppDataManager.dataBase lastError]description]);
        }
    }
    
    //打招呼表
    NSString * chatTableSayHi = [self getBackupTableName:TALKPEOPLESTABLE];
    query = [NSString stringWithFormat:@"CREATE table %@ AS SELECT * FROM %@",chatTableSayHi,TALKPEOPLESTABLE];
    
    bl = [MyAppDataManager.dataBase executeUpdate:query];
    if (!bl)
    {
        NSLog(@"backupDataBase :%@",[[MyAppDataManager.dataBase lastError]description]);
    }
    
    return YES;
}

//删除源数据库
+(BOOL)deleteOriginalDataBase
{
    if (![MyAppDataManager.dataBase goodConnection])
    {
        [MyAppDataManager.dataBase open];
    }
    
    //删除对话表
    NSString * query = [NSString stringWithFormat:@"DROP table %@ ",TALKPEOPLES];
    [MyAppDataManager.dataBase executeUpdate:query];
    
    //删除聊天记录表
    for (int i = 0 ; i < 10; i++)
    {
        NSString * oldChatTable = [MyAppDataManager getTableNameForUid:[NSString stringWithFormat:@"%d",i]];
        query = [NSString stringWithFormat:@"DROP table %@ ",oldChatTable];
        [MyAppDataManager.dataBase executeUpdate:query];
    }
    
    //删除打招呼表
    query = [NSString stringWithFormat:@"DROP table %@ ",TALKPEOPLESTABLE];
    
    [MyAppDataManager.dataBase executeUpdate:query];

    return YES;
}
//创建新的数据库
+(BOOL)initNewDataBase
{
    //对话表
    [MyAppDataManager initTalkPeopleDB];
    
    //聊天记录表
    [MyAppDataManager initDetailChatMessageTable];
    
    //打招呼表
    [MyAppDataManager initChatYetTable];
    
    return YES;
}

//将备份数据库中的数据拷贝到新数据库中
+(BOOL)copyDataToNewDB
{
    if (![MyAppDataManager.dataBase goodConnection])
    {
        [MyAppDataManager.dataBase open];
    }
    
    //对话表
    NSString * talkPeoplesTable = [self getBackupTableName:TALKPEOPLES];
    NSString * query = [NSString stringWithFormat:@"INSERT INTO %@ SELECT * FROM %@",TALKPEOPLES,talkPeoplesTable];
    
    BOOL bl = [MyAppDataManager.dataBase executeUpdate:query];
    if (!bl)
    {
        NSLog(@"copyDataToNewDB :%@",[[MyAppDataManager.dataBase lastError]description]);
    }
    
    //聊天记录表
    NSString * backupChatTable;
    for (int i = 0 ; i < 10; i++)
    {
        NSString * chatTable = [MyAppDataManager getTableNameForUid:[NSString stringWithFormat:@"%d",i]];
        backupChatTable = [self getBackupTableName:chatTable];
        query = [NSString stringWithFormat:@"INSERT INTO %@ SELECT * FROM %@",chatTable,backupChatTable];
        bl = [MyAppDataManager.dataBase executeUpdate:query];
        if (!bl)
        {
            NSLog(@"copyDataToNewDB :%@",[[MyAppDataManager.dataBase lastError]description]);
        }
    }
    
    //打招呼表
    NSString * chatTableSayHi = [self getBackupTableName:TALKPEOPLESTABLE];
    query = [NSString stringWithFormat:@"INSERT INTO %@ SELECT * FROM %@",TALKPEOPLESTABLE,chatTableSayHi];
    
    bl = [MyAppDataManager.dataBase executeUpdate:query];
    if (!bl)
    {
        NSLog(@"copyDataToNewDB :%@",[[MyAppDataManager.dataBase lastError]description]);
    }
    return bl;
}
//删除备份数据库
+(BOOL)deleteBackupDataBase
{
    if (![MyAppDataManager.dataBase goodConnection])
    {
        [MyAppDataManager.dataBase open];
    }
    
    //删除对话表
    NSString * talkPeoplesTable = [self getBackupTableName:TALKPEOPLES];
    NSString * query = [NSString stringWithFormat:@"DROP table %@ ",talkPeoplesTable];
    [MyAppDataManager.dataBase executeUpdate:query];
    
    //删除聊天记录表
    NSString * chatTable;
    for (int i = 0 ; i < 10; i++)
    {
        NSString * oldChatTable = [MyAppDataManager getTableNameForUid:[NSString stringWithFormat:@"%d",i]];
        chatTable = [self getBackupTableName:oldChatTable];
        query = [NSString stringWithFormat:@"DROP table %@ ",chatTable];
        [MyAppDataManager.dataBase executeUpdate:query];
    }
    
    //删除打招呼表
    NSString * chatTableSayHi = [self getBackupTableName:TALKPEOPLESTABLE];
    query = [NSString stringWithFormat:@"DROP table %@ ",chatTableSayHi];
    
    [MyAppDataManager.dataBase executeUpdate:query];
    
    return YES;
}
@end
